package com.cheyunkeji.er.adapter.auction;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.adapter.MyBaseAdapter;
import com.cheyunkeji.er.utils.ImageLoadUtil;
import com.cheyunkeji.er.utils.TimeUtils;
import com.cheyunkeji.er.Constants;
import com.cheyunkeji.er.bean.auction.BaojiaItem;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/** Created by Michael on 2017/2/16 15:45. */
public class BaojiaListViewAdapter extends MyBaseAdapter {

  public BaojiaListViewAdapter(ArrayList<BaojiaItem> dataList, Context context) {
    super(dataList, context);
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;
    if (convertView == null) {
      convertView = LayoutInflater.from(context).inflate(R.layout.er_lv_item_baojia, null);
      holder = new ViewHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    BaojiaItem itemBean = (BaojiaItem) dataList.get(position);
    ImageLoadUtil.loadImg(context, itemBean.getImgUrl(), holder.ivBaojiaItem);
    holder.tvCarBrand.setText(itemBean.getCarBrand());
    holder.tvDesc.setText(itemBean.getContentDesc());

    if (holder.tvPriceOrLeftTime.getVisibility() != View.VISIBLE) {
      holder.tvPriceOrLeftTime.setVisibility(View.VISIBLE);
    }
    //        根据报价Item的类型设置显示文本
    switch (itemBean.getItemType()) {
      case Constants.BAOJIA_TYPE_XUNJIA:
        holder.tvBaojiaState.setText("距离结束：");
        holder.tvPriceOrLeftTime.setText(TimeUtils.leftMillis2String(itemBean.getLeftTime()));
        break;
      case Constants.BAOJIA_TYPE_GIVE_ALREADY:
        holder.tvBaojiaState.setText("我的报价：");
        holder.tvPriceOrLeftTime.setText(itemBean.getMyGavePrice() + " 万元");
        break;
      case Constants.BAOJIA_TYPE_MISSED:
        holder.tvBaojiaState.setText("已结束");
        holder.tvPriceOrLeftTime.setVisibility(View.INVISIBLE);
        break;

      default:
        break;
    }
    return convertView;
  }

  static class ViewHolder {
    @BindView(R.id.iv_baojia_item)
    ImageView ivBaojiaItem;

    @BindView(R.id.iv_tag)
    ImageView ivTag;

    @BindView(R.id.tv_car_brand)
    TextView tvCarBrand;

    @BindView(R.id.tv_desc)
    TextView tvDesc;

    @BindView(R.id.tv_baojia_state)
    TextView tvBaojiaState;

    @BindView(R.id.tv_price_or_left_time)
    TextView tvPriceOrLeftTime;

    ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }
}
